package org.duiduo.controller;

import org.duiduo.common.bean.OrderVO;
import org.duiduo.common.request.OrderFindPageRequest;
import org.duiduo.common.response.Result;
import org.duiduo.common.valid.ValidationGroups;
import org.duiduo.service.OrderService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 订单相关控制层
 *
 * @author liangze
 */
@RestController
@RequestMapping("/order")
public class OrderController extends BaseController {

	@Resource
	private OrderService orderService;

	/**
	 * 跳转支付界面
	 *
	 * @param bookId
	 * @return
	 */
	@GetMapping("/buy/{bookId}")
	public Result<?> buy(@PathVariable("bookId") Long bookId) {
		// 新建订单，扣减库存
		return Result.success(orderService.buy(bookId, getUser()));
	}

	/**
	 * 支付成功
	 *
	 * @param orderVO
	 * @return
	 */
	@PostMapping("/buySuccess")
	public Result<?> buySuccess(@RequestBody @Validated(value = { ValidationGroups.Update.class }) OrderVO orderVO) {
		return Result.success(orderService.buySuccess(orderVO, getUser()));
	}

	/**
	 * 分页查询 - 如果不是管理员只能看到当前用户的订单
	 *
	 * @param request
	 * @return
	 */
	@PostMapping
	public Result<?> findPage(@RequestBody @Valid OrderFindPageRequest request) {
		return Result.success(orderService.findPage(getUser(), request));
	}

	/**
	 * 删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping("/{id}")
	public Result<?> delete(@PathVariable Integer id) {
		return Result.success(orderService.deleteById(id, getUser()));
	}
}
